package com.margo.project.consumer.rabbit.service;

import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import com.margo.project.consumer.common.model.EasydlDiscernMessageVO;
import com.rabbitmq.client.Channel;

/**
 * 配置错误
 * 
 * 1)	@RabbitListener(bindings= @QueueBinding(exchange=@Exchange("easydlDiscern"), value=@Queue("easyMask")))
 * 	inequivalent arg 'type' for exchange 'easydlDiscern' in vhost '/': 
 * 		received 'direct' but current is 'topic'
 * 
 * 2)	@RabbitListener(queues="easyMask",
 * 			bindings= @QueueBinding(exchange=@Exchange("easydlDiscern"),value=@Queue("easyMask")))
 * @RabbitListener can have only one of 'queues', 'queuesToDeclare', or 'bindings'
 * @author fxb208
 * 

 * 
 */
@Service
public class RabbitConsumerService {
	
	Logger logger=LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 指定exchange类型，默认为direct
	 * @param easydlDiscernMessageVO
	 * 
	 *  * 接收消息报错
	 *  org.springframework.messaging.converter.MessageConversionException: 
	 *  Cannot convert from [[B] to [com.margo.project.consumer.common.model.EasydlDiscernMessageVO] for GenericMessage 
	 *  [payload=byte[671878], 
	 *  headers={amqp_receivedDeliveryMode=PERSISTENT, 
	 *  	amqp_receivedRoutingKey=easyMask, 
	 *  	amqp_contentEncoding=UTF-8, 
	 *  	amqp_receivedExchange=easydlDiscern, 
	 *  	amqp_deliveryTag=2, 
	 *  	amqp_consumerQueue=easyMask, 
	 *  	amqp_redelivered=false, 
	 *  	id=20bf3039-5d49-2703-f623-8890b57aac20, 
	 *  	amqp_consumerTag=amq.ctag-MLMx-FNAcyZv-60dWKzQ7Q, 
	 *  	contentType=application/json, __TypeId__=com.easydl.project.amqp.model.EasydlDiscernMessageVO, 
	 *  	timestamp=1583898420577}]
	 * 没有设置序列化
	 */
	@RabbitListener(bindings= @QueueBinding(exchange=@Exchange(name="easydlDiscern",type="topic"), value=@Queue("easyMask"),key="easyMask"))
	public void messageConsumer(@Payload EasydlDiscernMessageVO easydlDiscernMessageVO,Channel channel, @Headers Map<String,Object> headers) {
		for(String str:headers.keySet()) {
			logger.info("rabbit:"+str);
		}
		//logger.info("rabbit收到消息:"+easydlDiscernMessageVO.toString());
	}
	
	/**
	 * 监听
	 * @param message
	 * @param channel
	 * @param headers
	 */
	@RabbitListener(bindings= @QueueBinding(exchange=@Exchange(name="margoRabbitExchange",type="topic"), value=@Queue("margoRabbitQueue"),key="margoRabbit"))
	public void messageNackConsumer(@Payload String message,Channel channel, @Headers Map<String,Object> headers) {
		Long tag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
		logger.info("tag:"+tag+"|message:"+message);
		try {
			channel.basicNack(tag,false,false);
		} catch (IOException e) {
			logger.info(e.getMessage(),e);
		}
	}
	
	/**
	 * 监听死信队列   direct交换器
	 * @param message
	 * @param channel
	 * @param headers
	 */
	@RabbitListener(queues="deadQueue")
	public void messageDeadConsumer(@Payload String message,Channel channel, @Headers Map<String,Object> headers) {
		Long tag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
		try {
			logger.info("死信队列确认消息tag:"+tag+"|message:"+message);
			channel.basicAck(tag,false);
		} catch (IOException e) {
			logger.info(e.getMessage(),e);
		}
	}
}
